<?php
namespace Kxdr\Controller;
use Think\Controller;
class ExtendController extends Controller {
	public $login;
	public $c;
	public $MixedNick;
	public $UserAvatar;
	public $UserPic;
	public $listRows = 10;
	public $rollPage = 5;
	public function  __construct (){
		parent::__construct();
		$top_parameters=I('get.top_parameters');
		$this->top_session=$top_session=I('get.top_session');
		$top_sign=I('get.top_sign');
		Vendor('taobaosdk.top.TopClient');
		Vendor('taobaosdk.lotusphp_runtime.Logger.Logger');
		Vendor('taobaosdk.top.RequestCheckUtil');
		Vendor('taobaosdk.top.request.ShopGetRequest');
		$this->c =  new \TopClient();
		$this->c->appkey=C("SM_KEY");
		$this->c->secretKey=C("SM_SECRET");
		// 移动端判断
		// if(ismobile()||$this->_param('mo')==1){ 
			// C('DEFAULT_THEME','MobTpl');
			// $L = D("Login");
			// $L->authString = $this->_param('authString'); 
			// $islogin = $L->QnMobLogin();
			// die();
			// $this->assign('authString',$L->authString);
			// $this->login = $L->QnMobLogin();
			
		// }
		session('[start]');
		session("access_token",$top_session);
		if(session("SID")&&session("VISITOR_NICK")&&session("VISITOR_ID")){
			$this->sid = session("SID");
			$this->usernick = session("VISITOR_NICK");
			$this->taobao_user_id =session("VISITOR_ID");
			$this->login=true;
		}else{
			$sign=base64_encode(md5(C('SM_KEY').$top_parameters.$top_session.C('SM_SECRET'),true));
			if($sign==$top_sign){
				$top_parameters=explode("&",base64_decode($top_parameters));
				// print_r($top_parameters);
				foreach($top_parameters as $parameter){
					$parameter = explode("=",$parameter);
					switch ($parameter[0]){
					case "ts":
						session("TIMESTEMP",(int)($parameter[1]));
						break;
					case "visitor_id":
						$this->taobao_user_id=$parameter[1];
						session("VISITOR_ID",$parameter[1]);
						break;
					case "visitor_nick":	
						$this->usernick=mb_convert_encoding($parameter[1], 'UTF-8', 'GB2312');
						session("VISITOR_NICK",mb_convert_encoding($parameter[1], 'UTF-8', 'GB2312'));
						break;
					case "refresh_token":
						$this->refresh_token=$parameter[1];
						session("refresh_token",$parameter[1]);
						break;	
					}
				}
				$req = new \ShopGetRequest;
				$req->setFields("sid");
				$req->setNick(session("VISITOR_NICK"));
				$resp = $this->c->execute($req);
				$this->sid = (string)$resp->shop->sid;
				$this->info='您已经成功登陆系统！';
				$this->login=true;
				if($this->sid){
					session("SID",$this->sid);
					$SHOP = M("ShopCfg");
					$shop = current($SHOP->where("`sid`='".$this->sid."'")->getField("`id`,`sid`,`sessionkey`,`taobao_user_id`,`usernick`,`logintime`,`jointime`"));
					if($shop){
						session("jointime",$shop["jointime"]);
						session("logintime",$shop["logintime"]);
						session("VISITOR_ID",$shop["taobao_user_id"]);
						session("VISITOR_NICK",$shop["usernick"]);
						$data["sessionkey"]=$this->top_session;
						$data["logintime"]=date("Y-m-d H:i:s",time());
						$SHOP->where("`sid`=".$this->sid)->save($data);
					}else{
						session("jointime",date("Y-m-d H:i:s",time()));
						session("logintime",date("Y-m-d H:i:s",time()));
						$data["sid"]=$this->sid;
						$data["sessionkey"]=$this->top_session;
						$data["refresh_token"]=$this->refresh_token;
						$data["usernick"]=$this->usernick;
						$data["jointime"]=date("Y-m-d H:i:s",time());
						$data["taobao_user_id"]=$this->taobao_user_id;
						$data["logintime"]=date("Y-m-d H:i:s",time());
						$SHOP->add($data);
					}
					$this->top_session = $this->top_session;
					$this->info='成功登录！';
					$this->login=true;
				}else{
					$this->info='读取店铺信息失败';
					$this->login=false;
				}
			}else{
				$this->info='请登录淘宝';
				$this->login=false;
			}
		}
		if(!$this->login){
			$this->assign('appkey',C("SM_KEY"));
			$this->display("Index:login");die();
		}
		if(!session("TOOL_STATUS")){
			$Chk = D("Shopadmin/CheckTools");
			$Chk->UserNick = $this->usernick;
			$Chk->IsvCode = C("SM_CODE");
			$Chk->CheckISV();
			if($Chk->tool["status"]==1){
				session("TOOL_STATUS",true);
			}else{
				session("TOOL_STATUS",false);
				$this->assign('menu',"shopadmin");
				$this->assign("current","home");
				$this->assign("info",$Chk->tool["info"]);
				$this->display("Index:dinggou");
				session('[start]');
				session('[destroy]');
				die();
			}
		}		
		$TOOL = D("ToolView");
		$version = $TOOL->where("`status`.`sid`=".$this->sid." AND `templet_code`='kxdr'")->getField("version");
		// 读取已选择的版本
		if($version){
			session("app_version",$version);
		}else{
			$version = C("DEFAULT_LANG");
			session("app_version",C("DEFAULT_LANG"));
		}
		cookie('think_language',$version); //设置默认版本
		$cfg_ver = C("VERSION");
		$this->assign('version',$cfg_ver[$version]);
		$this->safe = substr(md5($this->usernick),0,6);
		$this->assign("userid",session("VISITOR_ID"));
		$this->assign("usernick",session("VISITOR_NICK"));
		$this->assign("sid",session("SID"));
		$this->assign("jointime",session("jointime"));
		$this->assign("logintime",session("logintime"));
		$this->assign('info',$this->info);
		$this->assign('sid',$this->sid);
		$this->assign('L',L());
		$this->assign('CONTROLLER_NAME',CONTROLLER_NAME);
		$this->assign('menu',"shopadmin");
	}
	public function __destruct(){
		unset($this->c);
	}
	public function ajaxReturn($reda){ 
	// print_r($reda);
		$reda = json_encode(($reda));
		$re = I("get.callback")?I("get.callback").'('.$reda.')':$reda;
		echo($re);
	}
}